

if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
end

if ( CLIENT ) then

	SWEP.PrintName			= "SG550"			
	SWEP.Author				= "Counter-Strike"
	SWEP.Slot				= 2
	SWEP.SlotPos			= 0
	SWEP.IconLetter			= "o"
	
	killicon.AddFont( "weapon_sg550", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )
	
end

SWEP.HoldType			= "ar2"
SWEP.Base				= "weapon_cs_base2_snipe_ds"

SWEP.Spawnable			= true
SWEP.AdminSpawnable		= true

SWEP.ViewModel 			= "models/weapons/v_snip_sg550.mdl"
SWEP.WorldModel 		= "models/weapons/w_snip_sg550.mdl"

SWEP.Weight					= 5
SWEP.AutoSwitchTo			= false
SWEP.AutoSwitchFrom			= false

SWEP.Primary.Sound			= Sound("Weapon_SG550.Single")
SWEP.Primary.Recoil			= 0.5
SWEP.Primary.Damage			= 30
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.15
SWEP.Primary.ClipSize		= 20
SWEP.Primary.Delay			= 0.25
SWEP.Primary.DefaultClip	= 110
SWEP.Primary.Automatic		= true
SWEP.Primary.Ammo			= "smg1"

SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

function SWEP:PrimaryAttack()

	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
	
	if !self:CanPrimaryAttack() or self.Owner:WaterLevel() > 2 or self.Weapon:Clip1() < 1 then return end

	local LastShootTime = self.Weapon:GetNetworkedFloat( "LastShootTime", 0 )
	local Crouch = ( self.Owner:Crouching() and self.Owner:OnGround() ) and 2 or 1

	self:CSShootBullet( self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone / math.Clamp( ( CurTime() - LastShootTime ) * 1.3 , 0.001, 1 ) / Crouch )
	
	if self.Owner:IsNPC() then return end

	self.Owner:ViewPunch( Angle( math.Rand( -0.5, -0.1 ) * self.Primary.Recoil, math.Rand( -0.5, 0.5 ) *self.Primary.Recoil, 0 ) )

	if ( (SinglePlayer() && SERVER) || CLIENT ) then
		self.Weapon:SetNetworkedFloat( "LastShootTime", CurTime() )
	end
end

function SWEP:SecondaryAttack()
	self.Weapon:SetNextSecondaryFire( CurTime() + self.Secondary.Delay )
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Secondary.Delay )
	self.NextReload = CurTime() + self.Secondary.Delay

	if self.Weapon:GetNetworkedBool( "Zoomed", false ) then
		if self.Weapon:GetNetworkedInt( "Zoom", 8 ) == 4 then
				self.Weapon:SetNetworkedInt( "Zoom", 8 )
		else
			self.Weapon:SetNetworkedBool( "Zoomed", false )
			self.Weapon:SetNetworkedBool( "Scope", false )
			self.Weapon:SetNetworkedInt( "Zoom", 1 )
		end
	else
		self.Weapon:SetNetworkedBool( "Zoomed", true )
		self.Weapon:SetNetworkedBool( "Scope", true )
		self.Weapon:SetNetworkedInt( "Zoom", 4 )

	end
end

function SWEP:Think()
	if self.NeedToReload then
		self:Reload()
	end
	
	if self.Weapon:Clip1() < 1 and !self.Owner:KeyDown( IN_ATTACK ) and !self.AutoReloading then
		self:Reload()
		self.AutoReloading = true
		return
	end

	if self.Weapon:Clip1() > 0 then
		self.AutoReloading = false
	end

	if self.Weapon:GetNetworkedBool( "Zoomed", false ) then
		self.Primary.Cone = 0
		self.Primary.Recoil = 0.5
	else
		self.Primary.Cone = 0.15
		self.Primary.Recoil = 1
	end

	if CLIENT and self.Weapon:GetNetworkedBool( "Zoomed", false ) then
		self.MouseSensitivity = ( self.Owner:GetFOV() / 70 ) / 2 -- scale sensitivity
	else
		self.MouseSensitivity = 1
	end
end

function SWEP:Reload()
	if self.Owner:KeyDown( IN_ATTACK ) then
		self.NeedToReload = true
		return
	end
	self.NeedToReload = false
	self.Weapon:SetNetworkedBool( "Zoomed", false )
	self.Weapon:SetNetworkedBool( "Scope", false )
	self.Weapon:SetNetworkedInt( "Zoom", 1 )
	self.Weapon:DefaultReload( ACT_VM_RELOAD)
end
